package com.nettest.action.system;

import java.util.ArrayList;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;

import com.nettest.action.BaseJsonAction;
import com.nettest.entity.system.Role;
import com.nettest.pojo.TreeVo;
import com.nettest.service.system.RoleService;
import com.nettest.util.StringUtils;
@Action("roleManage")
@Namespace("/system/role")
@Results({@Result(name = "success", location = "/system/roleManage.jsp") })
public class RoleManageAction extends BaseJsonAction {

	private static final long serialVersionUID = -4740651262223282044L;
	private List<TreeVo> trees;
	private String id;
	private String roleId;
	private String operate;
	private Role role;
	@Autowired
	private RoleService roleService ;

	@Override
	public String execute() throws Exception {
		return SUCCESS;
	}
	@Action(value = "list", results = { @Result(name = "success", type = "json", params = {
			"ignoreHierarchy", "false", "excludeNullProperties", "true",
			"excludeProperties", "menu,menuService","root","trees" }) })
	public String list() throws Exception  {
		try {
			if(id == null) {
				List<Role> topMenu = roleService.getTopRole();
				transferVo(topMenu);
				TreeVo root = new TreeVo();
				root.setText("角色");
				root.setId("0");
				root.setState("open");
				root.setChildren(new ArrayList<TreeVo>(trees));
				trees = new ArrayList<TreeVo>();
				trees.add(root);
			} else {
				List<Role> subMenu = roleService.getSubRoles(id);
				transferVo(subMenu);
			}
		} catch (Exception e) {
			this.addActionMessage(e.getMessage());
		}
		return SUCCESS;
	}
	
	protected void transferVo(List<Role> roles) {
		trees = new ArrayList<TreeVo>();
		TreeVo treeVo = null;
		for (Role role : roles) {
			treeVo = new TreeVo();
			treeVo.setId(role.getRoleId());
			treeVo.setText(role.getRoleName());
			trees.add(treeVo);
		}
	}
	
	@Action(value = "edit",results={@Result(name = "success", location = "/system/roleEdit.jsp")})
	public String edit() throws Exception {
		operate = StringUtils.isBlank(operate) ? "editView" : operate;
		if (!StringUtils.isBlank(roleId)) {
			role = roleService.queryRoleById(roleId);
		}
		
		if ("insertSub".equalsIgnoreCase(operate)) {
			String parentRoleId = role.getRoleId();
			role.setRoleId(null);
			role.setParentId(parentRoleId);
			role.setRoleName(null);
			role.setMemo(null);
		}
		
		return SUCCESS;
	}
	@Action(value = "save",results={@Result(name = "success", location = "/system/roleEdit.jsp")})
	public String roleSave() throws Exception {
		if ("insertSub".equalsIgnoreCase(operate)) {
			roleService.insertRole(role);
		}
		else {
			roleService.updateRole(role);
		}
		
		return SUCCESS;
	}
	@Action(value = "delete",results={@Result(name = "success", location = "/system/roleEdit.jsp")})
	public String roleDelete() throws Exception {
		if (!StringUtils.isBlank(roleId)) {
			roleService.delRoleById(roleId);
		}
		
		return SUCCESS;
	}

	public String getRoleId() {
		return roleId;
	}

	public void setRoleId(String roleId) {
		this.roleId = roleId;
	}

	public String getOperate() {
		return operate;
	}

	public void setOperate(String operate) {
		this.operate = operate;
	}

	public Role getRole() {
		return role;
	}

	public void setRole(Role role) {
		this.role = role;
	}

	public RoleService getRoleService() {
		return roleService;
	}

	public void setRoleService(RoleService roleService) {
		this.roleService = roleService;
	}
	public List<TreeVo> getTrees() {
		return trees;
	}
	public void setTrees(List<TreeVo> trees) {
		this.trees = trees;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
}